package com.tmt.im.jpa.converter;


import com.tmt.im.common.enums.AddFriendMode;
import jakarta.persistence.AttributeConverter;
import jakarta.persistence.Converter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description TODO
 * @Author wuyi
 * @Date 2024/7/30 14:12
 * @Version 1.0
 **/

@Converter
public class StringAndAddFriendModeEnumListConverter implements AttributeConverter<List<AddFriendMode>, String> {

    @Override
    public String convertToDatabaseColumn(List<AddFriendMode> attribute) {
        if (attribute == null || attribute.size() <= 0) {
            return "";
        }
        return attribute.stream().map(Enum::name).reduce((i, o) -> i + "," + o).get();
    }

    @Override
    public List<AddFriendMode> convertToEntityAttribute(String dbData) {
        if (dbData == null || "".equals(dbData.trim())) {
            return new ArrayList<>();
        }

        return Arrays.stream(dbData.split(",")).filter(x -> x.length() > 0)
                .map(x -> Enum.valueOf(AddFriendMode.class, x)).collect(Collectors.toList());
    }
}
